package com.sphd.beautysalon.utils;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.sphd.beautysalon.interfaces.VolleyJsonArrayCallBack;
import com.sphd.beautysalon.base.RestaurantsApplication;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.interfaces.VolleyStringCallBack;

import java.util.Map;

/**
 * Created by Hu on 2016/9/27.
 */

public class VolleyUtil {
    //    private VolleyStringCallBack volleyStringCallBack;
//    private VolleyJsonObjectCallBack volleyJsonObjectCallBack;
//    private VolleyJsonArrayCallBack volleyJsonArrayCallBack;
    public void setStringRequest(String url, VolleyStringCallBack volleyStringCallBack) {
        Log.e("volley String=", url);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            Log.e("volley response=", response);
            volleyStringCallBack.onResponse(response);
        }, error -> {
            volleyStringCallBack.onErrorResponse(error);
            Log.e("volley error=", error.toString());
        });
        RestaurantsApplication.getRequestQueue().add(stringRequest);
    }

    public void setStringRequest(String url, VolleyStringCallBack volleyStringCallBack, Map<String, String> map) {
        Log.e("volley String=", url);
        Log.e("volley map=", map.toString());

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response ->
                volleyStringCallBack.onResponse(response),
                error ->
                        volleyStringCallBack.onErrorResponse(error)
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        RestaurantsApplication.getRequestQueue().add(stringRequest);
    }

    public void setJsonObjectRequest(String url, VolleyJsonObjectCallBack volleyJsonObjectCallBack, Map<String, String> map) {
        Log.e("MAP==", map.toString());
        Log.e("volley jsobj=", url);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, response -> {
            Log.e("volley json=", response.toString());
            volleyJsonObjectCallBack.onResponse(response);
        }, error -> {
            Log.e("volley error=", error.toString());
            volleyJsonObjectCallBack.onErrorResponse(error);
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        RestaurantsApplication.getRequestQueue().add(jsonObjectRequest);
    }


    public void setJsonObjectRequest(String url, VolleyJsonObjectCallBack volleyJsonObjectCallBack) {
        Log.e("volley jsobj=", url);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, response -> {
            Log.e("volley json=", response.toString());
            volleyJsonObjectCallBack.onResponse(response);
        }, error -> {
            Log.e("volley error=", error.toString());
            volleyJsonObjectCallBack.onErrorResponse(error);
        });
        RestaurantsApplication.getRequestQueue().add(jsonObjectRequest);
    }

    //
    public void setJsonArrayRequest(String url, VolleyJsonArrayCallBack volleyJsonArrayCallBack) {
        Log.e("volley jsobarray=", url);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, response ->
                volleyJsonArrayCallBack.onResponse(response), error ->
                volleyJsonArrayCallBack.onErrorResponse(error));
        RestaurantsApplication.getRequestQueue().add(jsonArrayRequest);
    }

    public void setJsonArrayRequest(String url, VolleyJsonArrayCallBack volleyJsonArrayCallBack, Map<String, String> map) {
        Log.e("volley jsobarray=", url);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, response ->
                volleyJsonArrayCallBack.onResponse(response), error ->
                volleyJsonArrayCallBack.onErrorResponse(error)) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        RestaurantsApplication.getRequestQueue().add(jsonArrayRequest);
    }


}
